#include "UTHUndefined.h"
#include "UTHFunction.h"

namespace phnq
{
	namespace uth
	{
		UTHFunction::UTHFunction(UTHFunctionListener *listener, const void *userData) : UTHValue(UTH_VALUE_TYPE_FUNCTION)
		{
			this->listener = listener;
			this->userData = userData;
			this->callback = NULL;
		}
	
		UTHFunction::UTHFunction(UTHValue* (*callback)(const void*,UTHArray*), const void *userData) : UTHValue(UTH_VALUE_TYPE_FUNCTION)
		{
			this->callback = callback;
			this->userData = userData;
			this->listener = NULL;
		}
	
		UTHFunction::~UTHFunction()
		{
		}

		const void* UTHFunction::getUserData()
		{
			return userData;
		}

		UTHValue* UTHFunction::invoke(UTHArray* args)
		{
			if(this->listener)
				return this->listener->invokeFunction(this, args);
			else if(this->callback)
				return this->callback(userData, args);
			else
				return UTHUndefined::getInstance();
		}
	
		void UTHFunction::serialize(stringbuf *sb)
		{
			const char *s = "function(){}";
			sb->sputn(s, strlen(s));
		}
	}
}
